package com.wuwei.poi.docx;

import org.apache.poi.sl.usermodel.TextParagraph;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.awt.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class DOCX {

    /**
     * 获取一个字符串有几个.
     * @param str
     * @return
     */
    public static int countDots(String str) {
        // 处理null情况，避免空指针异常
        if (str == null) {
            return 0;
        }
        int count = 0;
        // 遍历每个字符
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '.') { // 匹配点
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String url = "D:\\uploads\\OneDrive\\执照教材改版\\M8-TA教案\\M8-TA教案.docx";
        //读取office word
        try (XWPFDocument document = new XWPFDocument(new FileInputStream(url))) {
            List<XWPFTable> tables = document.getTables();
            for (XWPFTable table : tables) {
                //获取课时
                String hours = table.getRow(1).getCell(3).getText();
                //获取课程名称
                String title = table.getRow(0).getCell(1).getText();
                //获取大纲章节
                String chapter = table.getRow(1).getCell(1).getText() ;
                if (!chapter.contains("-")) {
                    System.out.println("error");
                    return;
                }
                int count = countDots(chapter);
                if (count==1) {
                    chapter = chapter.replace("-",".");
                } else if (count>1){
                    String substring1 = chapter.substring(0, chapter.indexOf("-"));
                    String substring2 = chapter.substring(chapter.indexOf("-") + 1);
                    substring2 = "00"+substring2;
                    chapter = substring1+"-"+substring2;
                    System.out.println(chapter);
                }
                chapter = "LL-P-M"+chapter;
                System.out.println(chapter);
                updatePPT(hours, title, chapter);
            }
        }  catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void updatePPT(String hours, String title, String chapter) {
        String newURL = "D:\\wuhu\\new";
        String oldURL = "D:\\uploads\\OneDrive\\执照教材改版\\M8-TA课件";
        System.out.println(chapter);
        if (chapter.contains("8.1")||chapter.contains("8.2")||chapter.contains("8.5")) {
            String substring = title.substring(title.indexOf(" ") + 1);
            if (chapter.contains("8.1")) {
                oldURL = oldURL + "\\8.1\\"+substring+".pptx";
                newURL = newURL + "\\8.1\\"+chapter+" "+substring+".pptx";
            } else if (chapter.contains("8.2")) {
                oldURL = oldURL + "\\8.2\\"+substring+".pptx";
                newURL = newURL + "\\8.2\\"+chapter+" "+substring+".pptx";
            } else if (chapter.contains("8.5")){
                oldURL = oldURL + "\\8.5\\"+substring+".pptx";
                newURL = newURL + "\\8.5\\"+chapter+" "+substring+".pptx";
            }
            //读取oldUrlppt
            EDITPPTX(oldURL, newURL,hours,title,chapter,substring);
        }
    }

    private static void EDITPPTX(String oldURL,String newURL,String hours, String title, String chapter,String substring) {
        try (FileInputStream fis = new FileInputStream(oldURL)) {
            XMLSlideShow ppt = new XMLSlideShow(fis);
            //获取ppt的第一张幻灯片
            XSLFSlide slide = ppt.getSlides().get(0);
            List<XSLFShape> shapes = slide.getShapes();
            for (XSLFShape shape : shapes) {
                if (shape.getClass().equals(XSLFTextBox.class)) {
                    XSLFTextBox textBox = (XSLFTextBox) shape;
                    if (textBox.getText().contains("学时")) {
                        textBox.setText("模块：M8\n"+"学时：" + hours);
                        setStyle(textBox,
                                VerticalAlignment.MIDDLE,   // 垂直居中
                                TextParagraph.TextAlign.LEFT,           // 水平居中
                                "微软雅黑",                 // 字体
                                24.0,                       // 字号
                                Color.WHITE.getRGB(),       // 字体颜色 (白色)
                                true);                     // 加粗);
                    }
                    else if (textBox.getText().contains("课件编号")){
                        textBox.setText("课件编号：" + chapter+"\n课件版本：R1");
                        setStyle(textBox,
                                VerticalAlignment.MIDDLE,   // 垂直居中
                                TextParagraph.TextAlign.LEFT,           // 水平居中
                                "黑体",                 // 字体
                                20.0,                       // 字号
                                Color.WHITE.getRGB(),       // 字体颜色 (白色)
                                true);
                    } else if (textBox.getText().contains("2025年10月9日")){

                    } else {
                        textBox.setText(substring);
                        setStyle(textBox,
                                VerticalAlignment.MIDDLE,   // 垂直居中
                                TextParagraph.TextAlign.CENTER,           // 水平居中
                                "微软雅黑",                 // 字体
                                54.0,                       // 字号
                                Color.WHITE.getRGB(),       // 字体颜色 (白色)
                                true);
                    }
                }
            }
            // 保存文件
            try (FileOutputStream out = new FileOutputStream(newURL)) {
                ppt.write(out);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setStyle(XSLFTextBox textBox, VerticalAlignment verticalAlignment, TextParagraph.TextAlign horizontalAlignment,
                                 String fontName, double fontSize, int fontColor, boolean isBold ) {
        textBox.setVerticalAlignment(verticalAlignment);
        List<XSLFTextParagraph> textParagraphs = textBox.getTextParagraphs();
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            for (XSLFTextRun textRun : textParagraph.getTextRuns()) {
                textRun.setFontFamily(fontName);
                textRun.setFontSize(fontSize);
                textRun.setFontColor(new Color(fontColor));
                textRun.setBold(isBold); // 使用 boolean 更清晰
            }
        }
    }
}
